﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour {
	public int m_speed;
	float h;
	float v;
	int force_move;
	Rigidbody rigidbody1;
	int ran;
	int IndexH1;
	int IndexV1;
	int IndexH2;
	int IndexV2;
	int randomh;
	int randomv;
	public GameObject table1;
	public GameObject table2;
	float m_time;

	void Start () {
		force_move = 300;
		m_speed = 50;
		rigidbody1 = gameObject.GetComponent<Rigidbody>();
	}
	
	// Update is called once per frame
	void Update () {
		/*h = Input.GetAxis("Horizontal") * Time.deltaTime * m_speed;
		v= Input.GetAxis("Vertical") * Time.deltaTime * m_speed;
		this.gameObject.transform.Translate(h, 0, v);*/
		if(m_time < 1)
		{
			m_time += Time.deltaTime;
		}
		else
		{
			m_time = 0;
			random ();
		}
		h = Input.GetAxis("Horizontal");
		v = Input.GetAxis("Vertical");
		if (h * randomh < 0)
			h = -h;

		if (v * randomv < 0)
			v = -v;
	}

	void FixedUpdate()
	{
		if(h>0f){
			rigidbody1.velocity = new Vector3 (m_speed, GetComponent<Rigidbody>().velocity.y,GetComponent<Rigidbody>().velocity.z);

		}else if(h<0f){
			rigidbody1.velocity = new Vector3 (-m_speed, GetComponent<Rigidbody>().velocity.y,GetComponent<Rigidbody>().velocity.z);
		}

		if(v>0f){
			rigidbody1.velocity = new Vector3 (GetComponent<Rigidbody>().velocity.x, GetComponent<Rigidbody>().velocity.y, m_speed);
		}else if(v<0f){
			rigidbody1.velocity = new Vector3 (GetComponent<Rigidbody>().velocity.x, GetComponent<Rigidbody>().velocity.y, -m_speed);
		}
	}

	void random()
	{
		IndexH1 = (transform.position.x < table1.gameObject.transform.position.x) ? 1 : -1;
		IndexV1 = (transform.position.z < table1.gameObject.transform.position.z) ? 1 : -1;
		IndexH2 = (transform.position.x < table2.gameObject.transform.position.x) ? 1 : -1;
		IndexV2 = (transform.position.z < table2.gameObject.transform.position.z) ? 1 : -1;

		ran  = Random.Range(0, 3);
		switch(ran)
		{
		case 0:
			{
				randomh = 0;
				randomv = 0;
				break;	
			}
		default:
			{
				randomh = IndexH1 + IndexH2;
				randomv = IndexV1 + IndexV2;
				break;
			}
		}
	}

}
